package Server.ServerCore.Controller;

import Server.ServerCore.Model.AI.BankerAI;
import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Service.DataPacks.packRespose.Bet_response;
import Server.ServerCore.Service.DataPacks.packRespose.cardBean_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.hardCode.CardType;
import Server.ServerCore.Service.usyns.SingelTableManager;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-11
 *         Time: 上午1:12
 *         cardTypeController =211
 *
 *         1.分牌；
 *         2.出现A，停止叫牌，则以不爆牌为点数；（这里没有处理bug）
 *         3.计算结余
 *         4.修改211，不发送任何数据；直接将会对庄家发牌
 *         5.庄家牌一次性发过来，当接收到数据就可以牌局结束，因为此时庄家的点数已经>=17了；
 *         6.庄家是A，但是发牌还没有结束，就弹出是否保险窗口，买保险数据包28可能出现问题
 *
 *         *从bug5.0的报告来看，主要是庄家在不同的情况下 发牌出现错误
 *
 *         当玩家返回接收到-1时，会返回此包，表示牌局结束，在每局中，只操作一次
 */
@Service
public class cardTypeController extends HandleController {
    private static final long serialVersionUID = 5824759644394179317L;

    private Logger logger= LoggerFactory.getLogger(cardTypeController.class);

    @Resource
    private SingelTableManager singelTableManager;
    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
            boolean haveInsurance=false;
            CardResultStatusVO[]cardResultStatusVOs=player.getDeckoOfCardsVOFastList();
            if (cardResultStatusVOs!=null){
                for (CardResultStatusVO statusVO:cardResultStatusVOs){
                    if (statusVO!=null&&statusVO.getInsurance()==1){
                        haveInsurance=true;//有买保险
                        break;
                    }
                }
                FastList<cardBean_response> cards_list=new FastList<cardBean_response>();
                if (player.atomicInteger.get()!=0||haveInsurance){
                    BankerAI bankerAI=singelTableManager.getBankerAI(player);
                    FastList<CardVO>AIcardVOs=bankerAI.getCardVOFastList();//bankerAI cardVO
                    CardVO vo=AIcardVOs.get(0);
                    int CardPoint=0;
                    if (vo.getCardType().getPoint()==0){
                        for (;;){
                            //如果大于17则break；否则循环产生牌，并且每次产生之后重新计算牌点数
                            if (CardPoint>=17){
                                bankerAI.setPoints(CardPoint);

                                CardPoint=0;
                                break;
                            }else {
                                CardPoint=0;
                                CardVO cardVO=new CardVO();
                                if (cardVO.getCardType().getPoint()==0){//A continue
                                    continue;
                                }
                                cardVO.setCircleUnitID(5);
                                AIcardVOs.add(cardVO);
                                //回复cardBean
                                cardBean_response cardBeanResponse=new cardBean_response();
                                cardBeanResponse.setCardType(cardVO.getCardType().getID());
                                cardBeanResponse.setCardcolour(cardVO.getCardcolour());
                                cardBeanResponse.setCircleUnitID(5);
                                cards_list.add(cardBeanResponse);

                                //添加一张牌后重新计算牌的点数
                                //计算点数，A+1；
                                for (CardVO c:AIcardVOs){
                                    if (c.getCardType().getPoint()==0){//A
                                        CardPoint+=1;
                                    }else {
                                        CardPoint+=c.getCardType().getPoint();
                                    }
                                }
                                //找A，+10；若大于21则不加10
                                for (CardVO c:AIcardVOs){
                                    if (c.getCardType().getPoint()==0){//A
                                        CardPoint=CardPoint+10>21?CardPoint:CardPoint+10;
                                    }
                                }
                            }
                        }
                    }else {
                        CardPoint=vo.getCardType().getPoint();
                        for (;;){
                            //如果大于17则break；否则循环产生牌，并且每次产生之后重新计算牌点数
                            if (CardPoint>=17){
                                bankerAI.setPoints(CardPoint);

                                CardPoint=0;
                                break;
                            }else {
                                CardPoint=0;
                                CardVO cardVO=new CardVO();
                                if (cardVO.getCardType().getPoint()==0){//A continue
                                    continue;
                                }
                                cardVO.setCircleUnitID(5);
                                AIcardVOs.add(cardVO);
                                //回复cardBean
                                cardBean_response cardBeanResponse=new cardBean_response();
                                cardBeanResponse.setCardType(cardVO.getCardType().getID());
                                cardBeanResponse.setCardcolour(cardVO.getCardcolour());
                                cardBeanResponse.setCircleUnitID(5);
                                cards_list.add(cardBeanResponse);
                                //添加一张牌后重新计算牌的点数
                                //计算点数，A+1；
                                for (CardVO c:AIcardVOs){
                                    if (c.getCardType().getPoint()==0){//A
                                        CardPoint+=1;
                                    }else {
                                        CardPoint+=c.getCardType().getPoint();
                                    }
                                }
                                //找A，+10；若大于21则不加10
                                for (CardVO c:AIcardVOs){
                                    if (c.getCardType().getPoint()==0){//A
                                        CardPoint=CardPoint+10>21?CardPoint:CardPoint+10;
                                    }
                                }
                            }
                        }
                    }
                    //推送数据 21
                    Bet_response betResponse=new Bet_response();
                    betResponse.setCards_list(cards_list);
                    player.DirectSend(betResponse.getJsonData(2,1));
                    cards_list.clear();
                }

            }



    }


    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[211]=this;
    }
}
